﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Artizan.Utility;

/// <summary>
/// Summary description for PomplingProperty
/// </summary>
public class PomplingProperty
{
    public int PomplingAddressID 
    {
        get;
        set;
    }
    public decimal PricePerCalendarMonth
    {
        get;
        set;
    }
    public string DisplayPicturePath
    {
        get;
        set;
    }
    public int NumberOfBedrooms
    {
        get;
        set;
    }
    public string ShortDescription
    {
        get;
        set;
    }
    public string LongDescription
    {
        get;
        set;
    }
    public string AddressLine1
    {
        get;
        set;
    }
    public string AddressLine2
    {
        get;
        set;
    }
     public string City
    {
        get;
        set;
    }
     public string County
    {
        get;
        set;
    }
     public string Postcode
    {
        get;
        set;
    }
     public DateTime DateAdded
    {
        get;
        set;
    }
     public bool Furnished
     {
         get;
         set;
     }

     public string PropertyType
     {
         get;
         set;
     }
     public int PropertyTypeID
     {
         get;
         set;
     }
     public string CustomerID
     {
         get;
         set;
     }

     public bool IsLet { get; set; }
	public PomplingProperty()
	{
        DisplayPicturePath = string.Empty;
	}

    public static List<PomplingProperty> SelectPageProperties(int offset, int listAmount)
    {
        List<PomplingProperty> propertiesList = new List<PomplingProperty>();
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomSelectPropertyPageRentalList", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pOffset", offset);
                cmd.Parameters.AddWithValue("@pReturnAmount", listAmount);
                cmd.Parameters.AddWithValue("@pOrderBy", " order by PricePerCalendarMonth ASC ");
                cmd.Parameters.AddWithValue("@pWhereClause", "");
                
                cmd.Connection.Open();                
                DataTable dt = new DataTable("DtPropertyList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertiesList = FieldMapper.DataTableToList<PomplingProperty>(ref dt);
            }
        }
        return propertiesList;
    }

    public static List<PomplingProperty> SelectCustomerAddressList(string PomplingCustomerID)
    {
        List<PomplingProperty> propertiesList = new List<PomplingProperty>();
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomSelectCustomerAddressList", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Connection.Open();
                cmd.Parameters.AddWithValue("@pPomplingCustomerID", PomplingCustomerID);
                DataTable dt = new DataTable("DtPropertyList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertiesList = FieldMapper.DataTableToList<PomplingProperty>(ref dt);
            }
        }
        return propertiesList;
    }

    public static List<PomplingProperty> SelectPageProperty(int webPropertyID)
    {
        List<PomplingProperty> propertiesList = new List<PomplingProperty>();
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomSelectRentalProperty", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pPomplingAddressID", webPropertyID);
                cmd.Connection.Open();

                DataTable dt = new DataTable("DtPropertyList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertiesList = FieldMapper.DataTableToList<PomplingProperty>(ref dt);
            }
        }       
        return propertiesList;
    }

    public bool SaveAddress(string pomplingCustomerID)
    {
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomPropertySave", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@pAddressID", this.PomplingAddressID);
                    cmd.Parameters.AddWithValue("@pAddressLine1", (string.IsNullOrEmpty(this.AddressLine1)?(object)DBNull.Value:this.AddressLine1));
                    cmd.Parameters.AddWithValue("@pAddressLine2", (string.IsNullOrEmpty(this.AddressLine2) ? (object)DBNull.Value : this.AddressLine2));
                    cmd.Parameters.AddWithValue("@pCity", (string.IsNullOrEmpty(this.City) ? (object)DBNull.Value : this.City));
                    cmd.Parameters.AddWithValue("@pCounty", (string.IsNullOrEmpty(this.County) ? (object)DBNull.Value : this.County));
                    cmd.Parameters.AddWithValue("@pPostcode", (string.IsNullOrEmpty(this.Postcode) ? (object)DBNull.Value : this.Postcode));
                    cmd.Parameters.AddWithValue("@pPomplingCustomerID", pomplingCustomerID);
                    cmd.Parameters.AddWithValue("@pPropIsLet", IsLet);
                    cmd.Connection.Open();
                    object o = cmd.ExecuteScalar();
                    int id = Int32.Parse(o.ToString());
                    this.PomplingAddressID = id;
                    cmd.Connection.Close();
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        return true;
    }

    public static PomplingProperty SelectAddress(int addressID)
    {
        List<PomplingProperty> propertiesList = new List<PomplingProperty>();
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomAddressSelectByPomplingAddressID", conn))
            {
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pPomplingAddressID", addressID);
                cmd.Connection.Open();

                DataTable dt = new DataTable("DtAddressList");
                IDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
                cmd.Connection.Close();
                propertiesList = FieldMapper.DataTableToList<PomplingProperty>(ref dt);
            }

            if (propertiesList.Count > 0)
                return propertiesList[0];

            return new PomplingProperty();
        }
    }

    public void SavePropertyLetinfo()
    {
        using (SqlConnection conn = new SqlConnection(Globals.GetConnectionString()))
        {
            using (SqlCommand cmd = new SqlCommand("PomPropertyLetInfoSave", conn))
            {
                try
                {                    
                    cmd.CommandType = System.Data.CommandType.StoredProcedure; //will not update, no WebPropertyID set
                    cmd.Parameters.AddWithValue("@pPomplingAddressID", this.PomplingAddressID);
                    cmd.Parameters.AddWithValue("@pPricePerCalendarMonth", this.PricePerCalendarMonth);   
                    cmd.Parameters.AddWithValue("@pNumberOfBedrooms", this.NumberOfBedrooms);                                     
                    cmd.Parameters.AddWithValue("@pFurnished", this.Furnished);
                    /*cmd.Parameters.AddWithValue("@pDisplayPicturePath", (string.IsNullOrWhiteSpace(this.DisplayPicturePath)) ? (object)DBNull.Value : this.DisplayPicturePath);*/
                    cmd.Parameters.AddWithValue("@pShortDescription", (string.IsNullOrWhiteSpace(this.ShortDescription)) ? (object)DBNull.Value : this.ShortDescription);
                    cmd.Parameters.AddWithValue("@pLongDescription", (string.IsNullOrWhiteSpace(this.LongDescription)) ? (object)DBNull.Value : this.LongDescription);
                    cmd.Parameters.AddWithValue("@pPropertyType", this.PropertyType);
                    cmd.Parameters.AddWithValue("@pCustomerID", this.CustomerID);
                    cmd.Connection.Open();
                    cmd.ExecuteScalar();                    
                    cmd.Connection.Close();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

    public static void Delete(int addressID, string customerID)
    {
        // DeleteAddress
        string connectionString = Globals.GetConnectionString();
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("PomArchiveAddress", conn))
            {
                try
                {
                    cmd.CommandType = System.Data.CommandType.StoredProcedure; //will not update, no WebPropertyID set
                    cmd.Parameters.AddWithValue("@pPomplingAddressID", addressID);
                    cmd.Parameters.AddWithValue("@pCustomerID", customerID);
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }    
}